import sys

sys.path.append("..")
import app
from common.request_tool import request, run
from model.dao_utils.original_covid19_map_wangyi_dao import *
from model.entity.original_covid19_map_wangyi import OriginalCovid19MapWangyi


header = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47 "
}


@run
@request(url="https://c.m.163.com/ug/api/wuhan/app/data/list-total", header=header)
def start(resp):
    rep_json = resp.json()
    if rep_json["code"] != 10000:
        app.App().log.error("网易疫情数据接口请求错误 url:[https://c.m.163.com/ug/api/wuhan/app/data/list-total] 内容:["+resp.text+"]")
        return
    area_tree = rep_json["data"]["areaTree"]
    # 全表更新 所以先截断数据表
    delete_all()
    for area in area_tree:
        sub_area_tree = area["children"]
        # 存储父区域数据
        en = OriginalCovid19MapWangyi()
        en.address_name = area["name"]
        en.total_confirm = area["total"]["confirm"]
        en.total_dead = area["total"]["dead"]
        en.total_heal = area["total"]["heal"]
        en.today_confirm = format_column_num(area["today"]["confirm"])
        en.today_dead = format_column_num(area["today"]["dead"])
        en.today_heal = format_column_num(area["today"]["heal"])
        en_id = insert(en)
        # 判断是否有子区域数据
        if len(sub_area_tree) < 2:
            continue
        # 处理子区域数据
        for sub_area in sub_area_tree:
            sub_sub_area_tree = sub_area["children"]
            # 存储子区域数据
            en = OriginalCovid19MapWangyi()
            en.parent_address_id = en_id
            en.address_name = sub_area["name"]
            en.total_confirm = sub_area["total"]["confirm"]
            en.total_dead = sub_area["total"]["dead"]
            en.total_heal = sub_area["total"]["heal"]
            en.today_confirm = format_column_num(sub_area["today"]["confirm"])
            en.today_dead = format_column_num(sub_area["today"]["dead"])
            en.today_heal = format_column_num(sub_area["today"]["heal"])
            sub_en_id = insert(en)
            # 判断是否有下级区域数据
            if len(sub_sub_area_tree) < 2:
                continue
            # 处理下级区域数据
            res_ens = []
            for sub_sub_area in sub_sub_area_tree:
                # 存储区域数据
                en = OriginalCovid19MapWangyi()
                en.parent_address_id = sub_en_id
                en.address_name = sub_sub_area["name"]
                en.address_name_en = ""
                en.total_confirm = sub_sub_area["total"]["confirm"]
                en.total_dead = sub_sub_area["total"]["dead"]
                en.total_heal = sub_sub_area["total"]["heal"]
                en.today_confirm = format_column_num(sub_sub_area["today"]["confirm"])
                en.today_dead = format_column_num(sub_sub_area["today"]["dead"])
                en.today_heal = format_column_num(sub_sub_area["today"]["heal"])
                res_ens.append(en)
            insert_all(res_ens)


def format_column_num(column):
    if column is None:
        return 0
    if type(column).__name__ == 'int' and column < 0:
        return 0
    return column
